/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-11-11
*/
package com.rzico.picture.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.picture.PictureUtils;
import com.rzico.picture.entity.Picture;
import com.rzico.picture.entity.PictureCategory;
import com.rzico.picture.service.PictureCategoryService;
import com.rzico.picture.service.PictureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 素材管理控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-11-11
 */
@Api(description = "素材管理接口")
@RestController
@RequestMapping("/admin/picture")
public class PictureAdminController extends BaseController {

    @Autowired
    private PictureService pictureService;

    @Autowired
    private PictureCategoryService pictureCategoryService;

    /**
     * 分页查询素材管理
     *
     * @return
    */
    @ApiOperation("分页查询素材管理")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pictureCategoryId", value = "分类", dataType = "String", paramType = "query")
    })
    public CommResult<Picture> list(Date startDate, Date endDate,Long pictureCategoryId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }

        if (pictureCategoryId!=null) {
            if (pictureCategoryId==0) {
                params.put("productCategoryId", pictureCategoryId);
            } else {
                params.put("treePath", String.valueOf(pictureCategoryId));
            }
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Picture> list = pictureService.selectList(params);
        PageResult<Picture> pageResult = new PageResult<Picture>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条素材管理
     *
     * @return
    */
    @ApiOperation("查询单条素材管理")
    @GetMapping("/find/{id}")
    public CommResult<Picture> find(@PathVariable String id) {
        Picture result = pictureService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("picture",result);

        if (result.getPictureCategoryId()!=null) {
            PictureCategory pictureCategory = pictureCategoryService.selectByPrimaryKey(result.getPictureCategoryId());
            data.put("pictureCategoryName",pictureCategory.getName());

        }

        return CommResult.success(data);

    }

    /**
     * 保存素材管理
     *
     * @param picture
     * @return
     */
    @ApiOperation("保存素材管理")
    @PostMapping("/save")
    public CommResult<Picture> save(@RequestBody Picture picture) {
        if (picture.getRw()==null) {
            picture.setRw(0);
        }
        if (picture.getDv()==null) {
            picture.setDv(0);
        }
        if (picture.getAlpha()==null) {
            picture.setAlpha(0.0);
        }
        if (picture.getAngle()==null) {
            picture.setAngle(0);
        }
        if (picture.getFontColor()==null) {
            picture.setFontColor("#ffffff");
        }
        if (picture.getFontSize()==null) {
            picture.setFontSize(32);
        }
        int affectCount = pictureService.insert(picture);
        if (affectCount <= 0) {
            return CommResult.error();
        }

        Picture result = pictureService.findById(picture.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除素材管理
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除素材管理", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除素材管理,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Picture> del(@PathVariable String ids) {

        pictureService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新素材管理
     *
     * @param picture
     * @return
     */
    @Log(desc = "更新素材管理", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新素材管理")
    @PostMapping("/update")
    public CommResult<Picture> update(@RequestBody Picture picture) {
        int affectCount = pictureService.updateByPrimaryKeySelective(picture);
        if (affectCount <= 0){
            return CommResult.error();
        }
        Picture result = pictureService.findById(picture.getId());
        return CommResult.success(result);
    }


    /**
     * 生成图片
     *
     * @return
     */
    @ApiOperation("生成图片")
    @GetMapping("/createImage/{id}")
    public CommResult<Picture> createImage(@PathVariable Integer id, String logoText) throws Exception{
        Map<String,Object> data = new HashMap<>();
        pictureService.createImage(id, logoText);

        return CommResult.success(data);

    }
}
